﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;
using UncleBobApplicantWebService.ApplicantWebService;
using UncleBobWebService.Database;


namespace UncleBobApplicantWebService.Controllers
{
    public class WelcomeController : Controller
    {
        //----------------------------------------------
        //----------------------------------------------
        private UncleBobApplicantWebService.ApplicantWebService.WebServiceClient mApplicantClient = new UncleBobApplicantWebService.ApplicantWebService.WebServiceClient();


        //**************************************************
        //Display register view
        //**************************************************
        public ActionResult Index()
        {           
            return View();
        }


        //**************************************************
        //Form action handle
        //**************************************************
        public ActionResult FormAction(FormCollection collection)
        {
            if (collection["register"] != null)
                return RedirectToAction("Register", "Register");
            else
            {
                RegisterModel user = new RegisterModel();
                Message msg = new Message();

                user.Username = collection["txtName"];
                user.Password = collection["txtPWord"];
                msg.Category = MessageTypes.SERVICE_GET_REGISTERED_USER;
                msg.Data = (Object)user;

                msg = mApplicantClient.RequestService(msg);

                if (msg.Result)
                {
                    switch (((RegisterModel)msg.Data).Permissions)
                    {
                        case PermissionTypes.User.Applicant:
                            Session.Add("User", msg.Data);
                            return RedirectToAction("MainMenu", "ApplicantMainMenu"); 
                           
                        case PermissionTypes.User.StaffingExpert:                            
                            return RedirectToAction("StaffingExpertMainMenu", "StaffingExpertMenu");

                        case PermissionTypes.User.HiringManager:
                            return RedirectToAction("MainMenu", "HMMainMenu");                             
                    }

                    return RedirectToAction("Error", "Error");
                }
                                       
            }
            return RedirectToAction("Index", "Welcome");
        }       
    }
}
